package ar.edu.uba.fi.jmit.monitorengine.core.monitor;

import java.util.Calendar;
import java.util.GregorianCalendar;

import ar.edu.uba.fi.jmit.monitorengine.MonitorEngineTestCase;

public class EventCountHelperTestCase extends MonitorEngineTestCase {

	public void test1() {
		EventCountHelper helper = new EventCountHelper();
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastDayCount());
		assertEquals(1, helper.getLastMonthCount());
		assertEquals(1, helper.getLastHourCount());
		assertEquals(1, helper.getLastMinuteCount());
		Calendar dateFrom = new GregorianCalendar();
		dateFrom.set(Calendar.DAY_OF_YEAR, new GregorianCalendar().get(Calendar.DAY_OF_YEAR) -1);
		assertEquals(1, helper.getCountFrom(dateFrom.getTime()));
	}
	
	public void testLastMonth() throws InterruptedException {
		EventCountHelper helper = new EventCountHelper();
		assertEquals(0, helper.getLastMonthCount());
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastMonthCount());
		helper.addReceivedEvent();
		assertEquals(2, helper.getLastMonthCount());
		
		for(int i = 0; i < 8; i++) {
			helper.addReceivedEvent();
		}
		assertEquals(10, helper.getLastMonthCount());
		
		helper = new EventCountHelper();
		assertEquals(0, helper.getLastMonthCount());
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 1);
		//calendar is last month plus one second
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(1, helper.getLastMonthCount());
		//sleep for more than a second
		Thread.sleep(1100);
		assertEquals(0, helper.getLastMonthCount());
		
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(0, helper.getLastMonthCount());
		
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastMonthCount());
	}
	
	public void testLastDay() throws InterruptedException {
		EventCountHelper helper = new EventCountHelper();
		assertEquals(0, helper.getLastDayCount());
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastDayCount());
		helper.addReceivedEvent();
		assertEquals(2, helper.getLastDayCount());
		
		for(int i = 0; i < 8; i++) {
			helper.addReceivedEvent();
		}
		assertEquals(10, helper.getLastDayCount());
		
		helper = new EventCountHelper();
		assertEquals(0, helper.getLastDayCount());
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 1);
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 1);
		//calendar is last day plus one second
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(1, helper.getLastDayCount());
		//sleep for more than a second
		Thread.sleep(1100);
		assertEquals(0, helper.getLastDayCount());
		
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(0, helper.getLastDayCount());
		
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastDayCount());
		
		assertEquals(3, helper.getTotalCount());
		assertEquals(3, helper.getLastMonthCount());
	}
	
	public void testLastHour() throws InterruptedException {
		EventCountHelper helper = new EventCountHelper();
		assertEquals(0, helper.getLastHourCount());
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastHourCount());
		helper.addReceivedEvent();
		assertEquals(2, helper.getLastHourCount());
		
		for(int i = 0; i < 8; i++) {
			helper.addReceivedEvent();
		}
		assertEquals(10, helper.getLastHourCount());
		
		helper = new EventCountHelper();
		assertEquals(0, helper.getLastHourCount());
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1);
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 1);
		//calendar is last day plus one second
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(1, helper.getLastHourCount());
		//sleep for more than a second
		Thread.sleep(1100);
		assertEquals(0, helper.getLastHourCount());
		
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(0, helper.getLastHourCount());
		
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastHourCount());
		
		assertEquals(3, helper.getTotalCount());
		assertEquals(3, helper.getLastMonthCount());
		assertEquals(3, helper.getLastDayCount());
	}
	
	public void testLastMinute() throws InterruptedException {
		EventCountHelper helper = new EventCountHelper();
		assertEquals(0, helper.getLastMinuteCount());
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastMinuteCount());
		helper.addReceivedEvent();
		assertEquals(2, helper.getLastMinuteCount());
		
		for(int i = 0; i < 8; i++) {
			helper.addReceivedEvent();
		}
		assertEquals(10, helper.getLastMinuteCount());
		
		helper = new EventCountHelper();
		assertEquals(0, helper.getLastMinuteCount());
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 1);
		calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 1);
		//calendar is last day plus one second
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(1, helper.getLastMinuteCount());
		//sleep for more than a second
		Thread.sleep(1100);
		assertEquals(0, helper.getLastMinuteCount());
		
		helper.addReceivedEvent(calendar.getTime());
		assertEquals(0, helper.getLastMinuteCount());
		
		helper.addReceivedEvent();
		assertEquals(1, helper.getLastMinuteCount());
		
		assertEquals(3, helper.getTotalCount());
		assertEquals(3, helper.getLastHourCount());
		assertEquals(3, helper.getLastDayCount());
		assertEquals(3, helper.getLastMonthCount());
	}
	
	public void testCountFrom() {
		EventCountHelper helper = new EventCountHelper();
		Calendar from = new GregorianCalendar();
		assertEquals(0, helper.getCountFrom(from.getTime()));
		helper.addReceivedEvent();
		assertEquals(1, helper.getCountFrom(from.getTime()));
		helper.addReceivedEvent();
		assertEquals(2, helper.getCountFrom(from.getTime()));
		
		for(int i = 0; i < 8; i++) {
			helper.addReceivedEvent();
		}
		assertEquals(10, helper.getCountFrom(from.getTime()));
		
		helper = new EventCountHelper();
		Calendar calendar = new GregorianCalendar();
		calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 1);
		helper.addReceivedEvent(calendar.getTime());
		from = new GregorianCalendar();
		assertEquals(0, helper.getCountFrom(from.getTime()));
		from.set(Calendar.HOUR, from.get(Calendar.HOUR) - 1);
		assertEquals(1, helper.getCountFrom(from.getTime()));
	}
}

